package com.pat.alevel;

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class A1010 {
	static Map<Character,  Integer> map = new HashMap();
	public static void main(String[] args) {
		Scanner cin = new Scanner(System.in);
		int i;
		String val1, val2, choose;
		long tag, radix, sum = 0, tempradix = 1, len;
		for(i = 0; i < 10; i++)
			map.put((char)(i + 48), i);
		for(i = 10; i <= 35; i++)
			map.put((char)(i + 87), i);
		val1 = cin.next();
		val2 = cin.next();
		tag = cin.nextInt();
		radix = cin.nextInt();
		choose = (tag == 1?val1:val2);
		sum = StringtoDecimal(choose, radix);
		choose = choose.equals(val1) ? val2 : val1;
		len = choose.length();
		char min = '0';
		for(i = 0; i < len; i++)
			if(choose.charAt(i) > min)
				min = choose.charAt(i);
		long mmin = min > '9' ? (int)min - 87 + 1:(int)min-48 + 1;
		long mmax = sum+1;
		while(mmin <= mmax) {
			tempradix = (mmin + mmax)/2;
			long sum2 = StringtoDecimal(choose, tempradix);
			if(sum2 < 0 || sum2 > sum) {
				mmax = tempradix - 1;
			}else if(sum2 < sum) {
				mmin = tempradix + 1;
			}else {
				System.out.println(tempradix);
				return;
			}
		}
		System.out.println("Impossible");
	}
	private static long StringtoDecimal(String str, long radix) {
		long len = str.length(), sum = 0;
		for(int i = 0; i < len; i++) {
			sum = (sum + map.get(str.charAt(i))) * radix;
			if(sum < 0)
				return -1;
		}
		return sum/radix;
	}
}